#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Module vu_type ."""

from string import Template
from tack.db import query, sql_string
from tack.lib import string_app, content_headers, status_codes, json_app
from tack.lib.jsontools import json_dumps
from tack.lib.dicttools import query_to_list
import time

# SQL type wrappers audited by Joshua Kugler, 2008-02-19

## TODO:  remove sql_statement_ prefixes  --Done bfrick
## TODO:  replace key variable names with meaningful names and match to regex  --Done bfrick
## TODO:  add module and function docstrings  --Done bfrick
## TODO:  use string.Template instead of string interpolation  --Done bfrick
## TODO:  replace json_dumps + string_app with json_ap  --Done bfrick

## SQL statements below

select_all_types = '''
SELECT vu_type_id, description
FROM tmpo_vu_type
'''

select_type_data = Template("""
SELECT vu_type_id, description
FROM tmpo_vu_type
WHERE vu_type_id = $vu_type_id
""")

insert_type = Template("""
INSERT INTO tmpo_vu_type (description)
VALUES ( '$description' )
""")

update_type_by_id = Template("""
UPDATE tmpo_vu_type
SET description='$description'
WHERE vu_type_id = $vu_type_id
""")

delete_type_by_id = Template("""
DELETE FROM tmpo_vu_type 
WHERE vu_type_id = $vu_type_id
""")

def SETFORM(environ,var):
    """Function SETFORM replaces multiple references to env['form'] with single form."""
    return environ['form'][var]

def ZIPLIST(labels,list):
    """Function ZIPLIST make a dictionary of two variables.  One is a list, the second
    is the labels for the lists.  
    ie; query_to_list([[1,2,3],[4,5,6],[7,8,9]],['one','two','three'])
    [{'three': 3, 'two': 2, 'one': 1}, {'three': 6, 'two': 5, 'one': 4}, {'three': 9, 'two': 8, 'one': 7}] """
    return [dict(zip(labels,i)) for i in list]

def get_all_types(environ, start_response):
    """Function get_all_types selects all rows."""
    sql_statement = select_all_types
    rows = query(environ['connection'], sql_statement)
    output = {}
    labels = ['id','description'] 
    output['types'] = ZIPLIST(labels, rows)
    return json_app(output)(environ, start_response)

def get_type_data(environ, start_response):
    """Function get_type_data select row by vu_type_id."""    
    sql_statement = select_type_data.substitute(
    vu_type_id = environ['dispatcher.groups']['vu_type_id']
    )
    rows = query(environ['connection'], sql_statement)
    output = {}
    labels = ['id','description'] 
    output['types'] = ZIPLIST(labels, rows)
    return json_app(output)(environ, start_response)

def add_type(environ, start_response):
    """Function add_type INSERTS row with form variables."""   
    sql_statement = insert_type.substitute(
    description = sql_string(SETFORM(environ,'description'))
    )
    query(environ['connection'], sql_statement)
    rows = query(environ['connection'], 'SELECT @@IDENTITY')
    return json_app(rows)(environ, start_response)

def update_type(environ, start_response):
    """Function update_type updates row with form variables and by vu_type_id."""       
    sql_statement = update_type_by_id.substitute(
     description = sql_string(SETFORM(environ,'description')),
     vu_type_id = environ['dispatcher.groups']['vu_type_id']
    )
    rows = query(environ['connection'], sql_statement)
    output = 'Type Updated'
    return json_app(output)(environ, start_response)

def delete_type(environ, start_response):
    """Function delete_type row by id."""    
    sql_statement = delete_type_by_id.substitute(
    vu_type_id = environ['dispatcher.groups']['vu_type_id']
    )
    rows = query(environ['connection'], sql_statement)
    output = 'Type Deleted'
    return json_app(output)(environ, start_response)
